﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FileConvertPdfSystemBLL;
using FileConvertPdfSystemModel;
using System.IO;

namespace FileConvertPdfSystemWeb
{
    public partial class LoginUserDownLoad : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string name=Convert.ToString(Session["UserNameLogin"]);
                bind(name);
            }
        }

        private void bind(string name)
        {
            LoginUserFileDown FileDown = new LoginUserFileDown();
            LoginUserFileDownBLL FileDownBLL = new LoginUserFileDownBLL();
            //FileDownBLL.Get_SingleLoginUserFileDown(Session["UserNameLogin"].ToString());

            //File_Share2 files = new File_Share2();
            GridView1.Visible = true;
            GridView1.DataSource = FileDownBLL.Get_LoginUserFileDown(name);

            GridView1.DataBind();

        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            //File_Share2 files = new File_Share2();
            //GridView1.DataSource = files.Get_File();
            //GridView1.DataBind();
            string name = Convert.ToString(Session["UserNameLogin"]);
            bind(name);

        }


        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            string storefolder = @"C:\" + "File_Convertpdf" + @"\" + "user" + @"\" + Convert.ToString(Session["UserNameLogin"]) + @"\";
            string storeuploadFile = storefolder + "upload" + @"\";
            FileInfo file1 = new FileInfo(GridView1.Rows[e.RowIndex].Cells[1].Text.ToString());
            string storedownFile = "";
            if (file1.Extension == ".xlsx"||file1.Extension==".xls")
            {   
                if (file1.Extension==".xlsx")
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".xlsx", ".pdf");
                }
                else
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".xls", ".pdf");
                }

            }
            if (file1.Extension == ".pptx"||file1.Extension==".ppt")
            {
                if (file1.Extension == ".pptx")
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".pptx", ".pdf");

                }else
                storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".ppt", ".pdf");

            }
            if (file1.Extension == ".docx"||file1.Extension==".doc")
            {
                if (file1.Extension == ".docx")
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".docx", ".pdf");

                }else
                storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".doc", ".pdf");

            }
            string path = storedownFile;

            FileInfo fi = new FileInfo(path);

            if (fi.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                Response.AddHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Filter.Close();
                Response.WriteFile(fi.FullName);
                Response.End();
            }
               
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string storefolder = @"C:\" + "File_Convertpdf" + @"\" + "user" + @"\" + Convert.ToString(Session["UserNameLogin"]) + @"\";
            string storeuploadFile = storefolder + "upload" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text;
            string path = storeuploadFile;

            FileInfo fi = new FileInfo(path);

            if (fi.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                Response.AddHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Filter.Close();
                Response.WriteFile(fi.FullName);
                Response.End();
            }


        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string storefolder = @"C:\" + "File_Convertpdf" + @"\" + "user" + @"\" + Convert.ToString(Session["UserNameLogin"]) + @"\";
            string storeuploadFile = storefolder + "upload" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text;
            string storedownFile = "";
            FileInfo file1 = new FileInfo(GridView1.Rows[e.RowIndex].Cells[1].Text.ToString());
            if (file1.Extension == ".xlsx" || file1.Extension == ".xls")
            {
                if (file1.Extension == ".xlsx")
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".xlsx", ".pdf");
                }
                else
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".xls", ".pdf");
                }

            }
            if (file1.Extension == ".pptx" || file1.Extension == ".ppt")
            {
                if (file1.Extension == ".pptx")
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".pptx", ".pdf");

                }
                else
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".ppt", ".pdf");

            }
            if (file1.Extension == ".docx" || file1.Extension == ".doc")
            {
                if (file1.Extension == ".docx")
                {
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".docx", ".pdf");

                }
                else
                    storedownFile = storefolder + "Download" + @"\" + GridView1.Rows[e.RowIndex].Cells[1].Text.Replace(".doc", ".pdf");

            }
            int id1=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            //int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[3].Text);
            LoginUserFileDownBLL LoginDowndel = new LoginUserFileDownBLL();
            LoginDowndel.Delete_LoginUserFileDown(id1);
            string name = Convert.ToString(Session["UserNameLogin"]);
            bind(name);

            System.IO.File.Delete(storedownFile);
            System.IO.File.Delete(storeuploadFile);

        }


    }
}